

function getSubmitCount(hid) {
    let count = $("#submitCount");
    $.ajax({
        type: "GET",
        url: "webapi/homework/getSubmitCount",
        data: {
            hid: hid
        }
    }).done(function (data){
        count.text(data);
    });
}

function loadTable(page) {
    let tid = $("#tid").val();
    $.ajax({
        type: "GET",
        url: "webapi/homework/getByPage",
        data: {
            page: page,
            tid: tid
        }
    }).done(function (pageUtil) {
        //console.log(pageUtil);
        let table = $("#table");
        let pagination = $("#pagination");
        //先清空表格
        table.empty();
        pagination.empty();
        let list = pageUtil.list;
        for (let key in list) {
            let homework = list[key];
            //console.log(homework);
            let id = $("<td>" + homework.id + "</td>");
            let title = $("<td>" + fixedStringLength(homework.title) + "</td>");
            let startDate = $("<td>" + formatDate(homework.startDate) + "</td>");
            let endDate = $("<td>" + formatDate(homework.endDate) + "</td>");
            let count = $("<td></td>");
            $.ajax({
                type: "GET",
                url: "webapi/homework/getSubmitCount",
                data: {
                    hid: homework.id
                }
            }).done(function (data) {
                count.text(data);
            });
            let button = $("<td></td>")
                .append($("<a class='button primary' href='toManageHomeworkDetail?hid=" + homework.id +"'>查看详情</a>"))
                .append($("<a class='button' onclick='deleteHomeworkById(" + homework.id + ")'>删除</a>"));
            let homeworkInfo = $("<tr></tr>").append(id).append(title).append(startDate).append(endDate).append(count).append(button);
            table.append(homeworkInfo);
        }
        //页码处理
        let current = pageUtil.current;
        let totalPage = pageUtil.totalPage;
        let total = pageUtil.total;
        pagination.append("<li><span class='button small' onclick='loadTable(0)'>首页</span></li>")
        if (current > 0){
            pagination.append("<li><span class='button small' onclick='loadTable(" + (current - 1) + ")'>上一页</span></li>");
        }
        for (let i = 0; i < totalPage; i++) {
            let pageNum = $("<li><span class='page' onclick='loadTable(" + i + ")'>" + (i + 1) + "</span></li>");
            if (i === current){
                pageNum.children("span").addClass("active");
            }
            pagination.append(pageNum);
        }
        if (current < totalPage - 1){
            pagination.append("<li><span class='button small' onclick='loadTable(" + (current + 1) + ")'>下一页</span></li>");
        }
        pagination.append("<li><span class='button small' onclick='loadTable(" + (totalPage - 1) + ")'>尾页</span></li>");
        pagination.append("<li>共有" + total + "个作业" + "</li>");
    })
}

function updateHomework() {
    let hid = $("#hid").val();
    let title = $("#title").val();
    //console.log(title);
    let tid = $("#tid").val();
    $.ajax({
        type: "POST",
        url: "webapi/homework/saveHomework",
        data: {
            hid: hid,
            title: title,
            tid: tid
        }
    }).done(function (data){
        if (data === 1){
            alert("保存成功");
        } else {
            alert("保存失败");
        }
    });
}

function deleteHomeworkById(hid) {
    if (confirm("确认删除？")){
        $.ajax({
            type: "DELETE",
            url: "webapi/homework/deleteHomework",
            data: {
                hid: hid
            }
        }).done(function (data){
            if (data === 1){
                alert("删除成功");
                loadTable(0);
            }
        });
    }
}

function addHomework(){
    let title = $("#title").val();
    let tid = $("#tid").val();
    $.ajax({
        type: "POST",
        url: "webapi/homework/saveHomework",
        data: {
            title: title,
            tid: tid
        }
    }).done(function (data){
        if (data === 1){
            alert("添加成功");
            window.location.href = "/toManageHomework";
        } else {
            alert("添加失败");
        }
    });
}